package com.qingyun.customview.hencode.practice1.views

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.toColorInt

class Practice1DrawHistogramView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr) {
    private val mPaint = Paint().apply {
        isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawColor("#506E7A".toColorInt())

        //画坐标轴
        val axis = Path()
        axis.moveTo(150f, 80f)
        axis.lineTo(150f, 500f)
        axis.lineTo(900f, 500f)
        mPaint.run {
            color = Color.WHITE
            style = Paint.Style.STROKE
            strokeWidth = 2f
        }
        canvas.drawPath(axis, mPaint)

        val p2 = Path()
        p2.addRect(170f, 490f, 250f, 500f, Path.Direction.CW)
        p2.addRect(270f, 430f, 350f, 500f, Path.Direction.CW)
        p2.addRect(370f, 430f, 450f, 500f, Path.Direction.CW)
        p2.addRect(470f, 380f, 550f, 500f, Path.Direction.CW)
        p2.addRect(570f, 350f, 650f, 500f, Path.Direction.CW)
        p2.addRect(670f, 300f, 750f, 500f, Path.Direction.CW)
        p2.addRect(770f, 400f, 850f, 500f, Path.Direction.CW)
        mPaint.run {
            style = Paint.Style.FILL
            color = Color.parseColor("#72B916")
        }
        canvas.drawPath(p2, mPaint)

        //文字
        mPaint.run {
            color = Color.WHITE
            textSize = 25f
        }
        canvas.drawText("Froyo", 170f, 520f, mPaint)
        canvas.drawText("GB", 290f, 520f, mPaint)
        canvas.drawText("ICS", 390f, 520f, mPaint)
        canvas.drawText("JB", 490f, 520f, mPaint)
        canvas.drawText("KitKat", 580f, 520f, mPaint)
        canvas.drawText("L", 7000f, 520f, mPaint)
        canvas.drawText("M", 800f, 520f, mPaint)

        mPaint.textSize = 40f
        canvas.drawText("直方图", 450f, 650f, mPaint)
    }
}